﻿Public Class PRICEFORMULA
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub PRICEFORMULA_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim objSelectMasformula As New masFormula

        Dim masformulaData As DataTable
        masformulaData = connect.GetTable(objSelectMasformula.selectMasformula_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        masformulaGrid.PrimaryGrid.DataSource = masformulaData

        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(FMID AS CHAR(4)) AS FMID, FMNAME FROM masformula WHERE STATUS = '1'"
        s = New FILTERCLASS(FMNAMEInMasdformulaTxt, sql, "รหัสสูตร,ชื่อสูตร", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

        Dim masdformulaData As DataTable
        masdformulaData = connect.GetTable(objSelectMasformula.selectMasdformula_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        masdformulaGrid.PrimaryGrid.DataSource = masdformulaData

        DesignForm(Me)
        DesignGrid(masformulaGrid)
        DesignGrid(masdformulaGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub


    Private Sub masformulaGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles masformulaGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value
        FMIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสูตร").Value()
        FMNAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อสูตร").Value()

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            masformulaStatusChk.Checked = True
        Else
            masformulaStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("masformula", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "STATUS", "FMID", FMIDTxt.Text)

        querymasdformula(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสูตร").Value())
    End Sub

    Public Sub querymasdformula(ByVal FMID As Integer)
        Dim masdformulaData As DataTable
        masdformulaData = connect.GetTable("SELECT a.`FMID` AS 'รหัสสูตร', b.`FMNAME` AS 'ชื่อสูตร', a.`DFMID` AS 'รหัสสูตรคำนวณ', a.`FORMULA` AS 'สูตรคำนวณ', a.`L_MARGIN` AS 'ต่ำสุด', a.`H_MARGIN` AS 'สูงสุด', a.`STATUS` AS 'สถานะการเปิดใช้งาน' FROM masdformula a JOIN masformula b ON a.`FMID` = b.`FMID` WHERE a.`FMID` = '" & FMID & "'")
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        masdformulaGrid.PrimaryGrid.DataSource = masdformulaData
    End Sub

    Private Sub FMNAMEInMasdformulaTxt_Click(sender As Object, e As EventArgs) Handles FMNAMEInMasdformulaTxt.Click
        FMNAMEInMasdformulaTxt.Text = ""
    End Sub

    Private Sub masdformulaGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles masdformulaGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value
        FMNAMEInMasdformulaTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสูตร").Value()
        FMNAMEInMasdformulaTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อสูตร").Value()
        L_MARGINDTxt.Text = CType(masdformulaGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ต่ำสุด").Value()


        H_MARGINDTxt.Text = CType(masdformulaGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สูงสุด").Value()
        FORMULARTxt.Text = CType(masdformulaGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สูตรคำนวณ").Value()
        DFMIDTxt.Text = CType(masdformulaGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสูตรคำนวณ").Value()

        If CType(masdformulaGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            masdformulaStatusChk.Checked = True
        Else
            masdformulaStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("masdformula", CType(masdformulaGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "STATUS", "DFMID", DFMIDTxt.Text)
    End Sub


    Private Sub FMNAMEInMasdformulaTxt_TextChanged(sender As Object, e As EventArgs) Handles FMNAMEInMasdformulaTxt.TextChanged
        querymasdformula(FMNAMEInMasdformulaTxt.Tag)
    End Sub


    Private Sub addMasformula_Click(sender As Object, e As EventArgs) Handles addMasformula.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If FMNAMETxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New masFormula
                    objInsertData.FMNAME_ = FMNAMETxt.Text
                    objInsertData.STATUS_ = masformulaStatusChk.Checked
                    objInsertData.insertMasformulaData()

                    Dim objSelectMasformula As New masFormula
                    Dim masformulaData As DataTable
                    masformulaData = connect.GetTable(objSelectMasformula.selectMasformula_)
                    'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                    masformulaGrid.PrimaryGrid.DataSource = masformulaData

                    FMNAMETxt.Text = ""
                    FMIDTxt.Text = ""
                    masformulaStatusChk.Checked = False
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasformula_Click(sender As Object, e As EventArgs) Handles editMasformula.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If FMIDTxt.Text.ToString.Length > 0 And FMNAMETxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลชื่อสูตรการคำนวณราคาดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        Dim objEditData As New masFormula
                        objEditData.FMID_ = FMIDTxt.Text
                        objEditData.FMNAME_ = FMNAMETxt.Text
                        objEditData.STATUS_ = masformulaStatusChk.Checked
                        objEditData.editMasformulaData()

                        Dim objSelectMasformula As New masFormula
                        Dim masformulaData As DataTable
                        masformulaData = connect.GetTable(objSelectMasformula.selectMasformula_)
                        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                        masformulaGrid.PrimaryGrid.DataSource = masformulaData

                        FMNAMETxt.Text = ""
                        FMIDTxt.Text = ""
                        masformulaStatusChk.Checked = False
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub addMasdformula_Click(sender As Object, e As EventArgs) Handles addMasdformula.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If FMNAMEInMasdformulaTxt.Tag.ToString.Length > 0 And L_MARGINDTxt.Text.ToString.Length > 0 And FORMULARTxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New masFormula

                    If H_MARGINDTxt.Text = "" Then
                        objInsertData.H_MARGIN_ = 99999999
                    Else
                        objInsertData.H_MARGIN_ = H_MARGINDTxt.Text
                    End If

                    If objInsertData.H_MARGIN_ > L_MARGINDTxt.Text Then
                        objInsertData.FMID_ = FMNAMEInMasdformulaTxt.Tag
                        objInsertData.L_MARGIN_ = L_MARGINDTxt.Text
                        'objInsertData.H_MARGIN_ = H_MARGINDTxt.Text
                        objInsertData.FORMULA_ = FORMULARTxt.Text
                        objInsertData.STATUS_ = masdformulaStatusChk.Checked
                        objInsertData.insertMasdformulaData()

                        Dim objSelectMasformula As New masFormula
                        Dim masdformulaData As DataTable
                        masdformulaData = connect.GetTable(objSelectMasformula.selectMasdformula_)
                        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                        masdformulaGrid.PrimaryGrid.DataSource = masdformulaData

                        DFMIDTxt.Text = ""
                        FMNAMEInMasdformulaTxt.Text = ""
                        FMNAMEInMasdformulaTxt.Tag = ""
                        L_MARGINDTxt.Text = ""
                        H_MARGINDTxt.Text = ""
                        FORMULARTxt.Text = ""
                        masdformulaStatusChk.Checked = False
                    Else
                        MsgBox("ไม่สามารถระบุค่าสูงสุดให้น้อยกว่าค่าต่ำสุดได้ หากไม่มีค่าสูงสุดให้เว้นว่างไว้")
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน (ค่าสูงสุดสามารถเว้นได้)")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasdformula_Click(sender As Object, e As EventArgs) Handles editMasdformula.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If DFMIDTxt.Text.ToString.Length > 0 And FMNAMEInMasdformulaTxt.Tag.ToString.Length > 0 And L_MARGINDTxt.Text.ToString.Length > 0 And FORMULARTxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลสูตรการคำนวณราคาดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        Dim objEditData As New masFormula

                        If H_MARGINDTxt.Text = "" Then
                            objEditData.H_MARGIN_ = 99999999
                        Else
                            objEditData.H_MARGIN_ = H_MARGINDTxt.Text
                        End If

                        If objEditData.H_MARGIN_ > L_MARGINDTxt.Text Then
                            objEditData.DFMID_ = DFMIDTxt.Text
                            objEditData.FMID_ = FMNAMEInMasdformulaTxt.Tag
                            objEditData.L_MARGIN_ = L_MARGINDTxt.Text
                            'objEditData.H_MARGIN_ = H_MARGINDTxt.Text
                            objEditData.FORMULA_ = FORMULARTxt.Text
                            objEditData.STATUS_ = masdformulaStatusChk.Checked
                            objEditData.editMasdformulaData()

                            Dim objSelectMasformula As New masFormula
                            Dim masdformulaData As DataTable
                            masdformulaData = connect.GetTable(objSelectMasformula.selectMasdformula_)
                            'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                            masdformulaGrid.PrimaryGrid.DataSource = masdformulaData

                            DFMIDTxt.Text = ""
                            FMNAMEInMasdformulaTxt.Text = ""
                            FMNAMEInMasdformulaTxt.Tag = ""
                            L_MARGINDTxt.Text = ""
                            H_MARGINDTxt.Text = ""
                            FORMULARTxt.Text = ""
                            masdformulaStatusChk.Checked = False
                        Else
                            MsgBox("ไม่สามารถระบุค่าสูงสุดให้น้อยกว่าค่าต่ำสุดได้ หากไม่มีค่าสูงสุดให้เว้นว่างไว้")
                        End If
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน (ค่าสูงสุดสามารถเว้นได้)")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    
   
End Class